﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Orgs;

namespace SmartSpace.WebApp.Modules.Orgs.Pickers
{
    public partial class OrgPicker : SmartSpace.Web.SsPicker
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OrgsService service = new OrgsService();
                List<Node> list = service.GetChildren(Guid.Empty, true);
                foreach (var menu in list)
                {
                    var tNode = new TreeNode(menu.Name, menu.Id.ToString());
                    this.Org_Tree.Nodes.Add(tNode);
                    tNode.NavigateUrl = string.Format("javascript:window.returnValue={0}value:'{1}', text:'{2}'{3};window.close();", "{", menu.Id, menu.Name, "}");
                    LoadTree(tNode, menu);
                }
            }
        }

        private void LoadTree(TreeNode node, Node orgNode)
        {
            foreach (var menu in orgNode.Children)
            {
                var tNode = new TreeNode(menu.Name, menu.Id.ToString());
                tNode.NavigateUrl = string.Format("javascript:window.returnValue={0}value:'{1}', text:'{2}'{3};window.close();", "{", menu.Id, menu.Name, "}");
                node.ChildNodes.Add(tNode);
                LoadTree(tNode, menu);
            }
        }
    }
}